package org.x.user.dubbo;

// 新增导入
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.x.api.system.dto.UserDTO;
import org.x.api.system.feign.RemoteUserService;
import org.x.user.entity.SysUsers;
import org.x.user.mapper.SysUsersMapper;
import java.util.stream.Collectors;
import java.util.List;

@DubboService(version = "1.0.0", group = "x-user")
public class RemoteUserServiceImpl implements RemoteUserService {

    @Autowired
    private SysUsersMapper userMapper;

    @Override
    public UserDTO getUserById(Long userId) {
        SysUsers user = userMapper.selectById(userId);
        return convertToDTO(user);
    }

    @Override
    public UserDTO getUserByUsername(String username) {
        SysUsers user = userMapper.selectOne(
                new QueryWrapper<SysUsers>().eq("username", username)
        );
        return convertToDTO(user);
    }

    @Override
    public List<UserDTO> searchUsers(String keyword) {
        QueryWrapper<SysUsers> wrapper = new QueryWrapper<>();
        wrapper.like("username", keyword)
                .or().like("nick_name", keyword)
                .or().like("email", keyword);

        return userMapper.selectList(wrapper)
                .stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private UserDTO convertToDTO(SysUsers user) {
        if (user == null) return null;
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        return userDTO;
    }
}